package com.hlhj.carview.aty

import android.graphics.Color
import android.support.v7.widget.LinearLayoutManager
import android.view.LayoutInflater
import android.view.View
import com.hlhj.carview.BaseAty
import com.hlhj.carview.Iview.NothingView
import com.hlhj.carview.LoadtNet
import com.hlhj.carview.R
import com.hlhj.carview.adapter.JudgeListAdp
import com.hlhj.carview.bean.ArticalBean
import com.hlhj.carview.bean.JudgeListBean
import com.hlhj.carview.bean.MyBaseBean
import com.hlhj.carview.customView.AndroidBug5497Workaround
import com.hlhj.carview.http.CustomAction
import com.hlhj.carview.http.MyJsonConvert
import com.hlhj.carview.http.Urls
import com.hlhj.carview.presenter.NothingPresenter
import com.hlhj.carview.utils.KeyboardUtil
import com.lzy.okgo.OkGo
import com.lzy.okgo.model.HttpParams
import com.lzy.okgo.model.Response
import com.lzy.okrx2.adapter.ObservableResponse
import io.reactivex.schedulers.Schedulers
import kotlinx.android.synthetic.main.aty_news_detail.*
import kotlinx.android.synthetic.main.judge_head.view.*
import kotlinx.android.synthetic.main.lo_pubhead.*

/**
 *NeverMore
 *2020/9/15
 **/
class JudgeAty:BaseAty<NothingView,NothingPresenter>(), NothingView {
    private val datas=ArrayList<JudgeListBean.Data>()
    private lateinit var adp:JudgeListAdp
    private lateinit var headView:View
    private lateinit var eptView:View

    override fun getContentID(): Int {
        return R.layout.aty_news_detail
    }

    override fun bindView(): NothingView {
        return this
    }

    override fun createPresenter(): NothingPresenter {
        return NothingPresenter()
    }

    override fun initView() {
        AndroidBug5497Workaround.assistActivity(this)

        adp=JudgeListAdp(datas)
        listView.layoutManager=LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false)
        listView.adapter=adp

        eptView=LayoutInflater.from(this).inflate(R.layout.no_comment,null)

        headView=LayoutInflater.from(this).inflate(R.layout.judge_head,null)

        adp.addHeaderView(headView)
        headView.aTitle.text=intent.getStringExtra("title")
        headView.aImg.LoadtNet(intent.getStringExtra("icon"))
        headView.aSource.text="雷锋网"
        headView.aTime.text=intent.getStringExtra("time")
        headView.aContent.text=intent.getStringExtra("content")

        val httpParams = HttpParams()
        httpParams.put("uriPath","judgeList")
        httpParams.put("aid",intent.getStringExtra("aid"))

        OkGo.post<JudgeListBean>(Urls.PubUrl)
                .headers("type",3.toString())
                .params(httpParams)
                .converter(object : MyJsonConvert<JudgeListBean>(){})
                .adapt(ObservableResponse<JudgeListBean>())
                .subscribeOn(Schedulers.io())
                .subscribe(object : CustomAction<Response<JudgeListBean>>(this){
                    override fun httpSuccess(t: Response<JudgeListBean>) {
                        t.body().data?.let { it1 -> datas.addAll(it1) }
                        adp.notifyDataSetChanged()
                    }
                })
        adp.setEmptyView(eptView)
        adp.setHeaderAndEmpty(true)

    }

    override fun logicStart() {

        btExit.setColorFilter(Color.BLACK)
        tvTittle.text="咨询详情"
        tvTittle.setTextColor(Color.BLACK)
    }

    override fun setListener() {
        btSend.setOnClickListener {
            val httpParams = HttpParams()
            httpParams.put("uriPath","judgeList")
            httpParams.put("aid",intent.getStringExtra("aid"))
            httpParams.put("name","测试员")
            httpParams.put("timeStamp",(System.currentTimeMillis()/1000).toString())
            httpParams.put("content",etContent.text.toString())
            OkGo.post<MyBaseBean>(Urls.PubUrl)
                    .headers("type",1.toString())
                    .params(httpParams)
                    .converter(object : MyJsonConvert<MyBaseBean>(){})
                    .adapt(ObservableResponse<MyBaseBean>())
                    .subscribeOn(Schedulers.io())
                    .subscribe(object : CustomAction<Response<MyBaseBean>>(this){
                        override fun httpSuccess(t: Response<MyBaseBean>) {
                            etContent.setText("")
                            val params = HttpParams()
                            params.put("uriPath","judgeList")
                            params.put("aid",intent.getStringExtra("aid"))
                            KeyboardUtil.closeKeyboard(etContent)
                            OkGo.post<JudgeListBean>(Urls.PubUrl)
                                    .headers("type",3.toString())
                                    .params(params)
                                    .converter(object : MyJsonConvert<JudgeListBean>(){})
                                    .adapt(ObservableResponse<JudgeListBean>())
                                    .subscribeOn(Schedulers.io())
                                    .subscribe(object : CustomAction<Response<JudgeListBean>>(this@JudgeAty){
                                        override fun httpSuccess(t: Response<JudgeListBean>) {
                                            datas.clear()
                                            t.body().data?.let { it1 -> datas.addAll(it1) }
                                            datas.reverse()
                                            adp.notifyDataSetChanged()
                                        }
                                    })
                        }
                    })

        }
    }
}